using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using DAL;
using BLL;
using DTO;

namespace WellmeadowsHospital
{
    public partial class frmListStaffReport : DevExpress.XtraEditors.XtraUserControl
    {
        StaffBLL staffBLL = new StaffBLL();
        AllocatedBLL allocatedBLL = new AllocatedBLL();
        StaffOfWardBLL sowBLL = new StaffOfWardBLL();
        public frmListStaffReport()
        {
            InitializeComponent();
            dewsaBeginDate.DateTime = DateTime.Now;
            BindingData1();
            BindingData2();
            
        }
        public void BindingData1()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = sowBLL.ListStaffInWardDetails(frmMain.bsiwardid);
            navStaff.BindingSource = bs;
            gcWardStaffAllocation.DataSource = bs;
        }
        public void BindingData2()
        {
            BindingSource bs1 = new BindingSource();
            bs1.DataSource = sowBLL.WardStaffList(frmMain.bsiwardid);
            navStaffInList.BindingSource = bs1;
            gcWardStaffList.DataSource = bs1;
        }
        public void BindingData3()
        {
            BindingSource bs1 = new BindingSource();
            bs1.DataSource = sowBLL.WardStaffList2(frmMain.bsiwardid, Convert.ToDateTime(dateDateChose.DateTime.ToString().Split()[0]));
            navStaffInList.BindingSource = bs1;
            gcWardStaffList.DataSource = bs1;
        }


        private void btnWardStaffAllocation_Click(object sender, EventArgs e)
        {
            AllocatedDTO all = new AllocatedDTO();
            // all.WardId = bsiWardID.Caption;
            all.WardId = frmMain.bsiwardid;
            all.StaffId = txtwsaStaffID.Text;
            all.Shift = cbbwsaAllocation.SelectedItem.ToString();
            all.DateBegin = dewsaBeginDate.DateTime;
            string msg = allocatedBLL.Insert(all);
            if (msg.Equals("success"))
            {

                MessageBox.Show("Insert successul");
                BindingData1();
                BindingData2();
            }
            else
            {
                MessageBox.Show("Error: "+msg);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Report r = new Report();

            r.DataSource = sowBLL.WardStaffList2(frmMain.bsiwardid, Convert.ToDateTime(dateDateChose.DateTime.ToString().Split()[0]));
            string[] info = sowBLL.WardChargeNurseInfo(frmMain.bsiwardid);
            r.Load(info[0], info[1]);

            r.ShowPreviewDialog();
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            gcWardStaffAllocation.DataSource = staffBLL.SearchIDName(txtSearch.Text);
        }

        private void gvWardStaffAlllocation_RowClick_1(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtwsaStaffID.Text = gvWardStaffAlllocation.GetRowCellValue(gvWardStaffAlllocation.GetSelectedRows()[0], gcWSAStaffID).ToString();
            txtwsaStaffName.Text = gvWardStaffAlllocation.GetRowCellValue(gvWardStaffAlllocation.GetSelectedRows()[0], gcwsaName).ToString();
            if (gvWardStaffAlllocation.GetRowCellValue(gvWardStaffAlllocation.GetSelectedRows()[0], gcwsaImageProfile) != null)
            {

                // Binary data from MS SQL is received in Base64 format. So you can decode it using the FromBase64String function
                byte[] a = Convert.FromBase64String(gvWardStaffAlllocation.GetRowCellValue(gvWardStaffAlllocation.GetSelectedRows()[0], gcwsaImageProfile).ToString().Replace("\"", ""));
                MemoryStream ms = new MemoryStream(a);
                Image image = Image.FromStream(ms);
                peWardStaffAllocation.EditValue = image;
                //can bang hinh anh trong pictureEdit 
                peWardStaffAllocation.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
            }
            else
            {
                peWardStaffAllocation.EditValue = "";
            }
        }

        private void dewsaBeginDate_EditValueChanged(object sender, EventArgs e)
        {
            dateDateChose.EditValue = dewsaBeginDate.DateTime;
        }

        private void btnNewAllocation_Click(object sender, EventArgs e)
        {
            MessageBox.Show(gvWardSatffList.RowCount.ToString());
            for (int i = 0; i <= gvWardSatffList.RowCount; i++)
            {
               
                    if (gvWardSatffList.GetRowCellValue(i, gcAllocationStaffID) != null)
                {
                    //MessageBox.Show(gvWardSatffList.GetRowCellValue(i, gcAllocationStaffID).ToString()+i);
                    AllocatedDTO all = new AllocatedDTO();
                    all.StaffId = gvWardSatffList.GetRowCellValue(i, gcAllocationStaffID).ToString();
                    all.WardId = frmMain.bsiwardid;
                    all.DateBegin = Convert.ToDateTime(gvWardSatffList.GetRowCellValue(i, gcAllocationDateBegin));

                    if(allocatedBLL.Update(all).Equals("success")){                        
                        BindingData1();
                        BindingData2();
                    }else{
                        MessageBox.Show("fail");
                    }
                }
                    
                }
                //int rowHandle = gvWardSatffList.GetVisibleRowHandle(i);
                
            //}
            MessageBox.Show("Begin allocation");
        }

        private void dateDateChose_EditValueChanged(object sender, EventArgs e)
        {
            BindingData1();
            BindingData3();
        }
    }
}
